<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
     version="1.0"
     xmlns="http://www.imsglobal.org/xsd/imscc/imscp_v1p1"
     xmlns:lom="http://ltsc.ieee.org/xsd/LOM"
     xmlns:lomcc="http://ltsc.ieee.org/xsd/imscc/LOM"
     xmlns:voc="http://ltsc.ieee.org/xsd/LOM/vocab"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:sco="http://www.imsglobal.org/xsd/imscp_v1p1">
  <xsl:variable name="default-org" select="sco:manifest/sco:organizations/@default" />
    
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <!-- first up, match the manifest itself -->
    <xsl:apply-templates select="/sco:manifest" />
  </xsl:template>

  <xsl:template match="sco:manifest">
    <manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.imsglobal.org/xsd/imscc/imscp_v1p1 imscp_v1p2_localised.xsd
                                  http://ltsc.ieee.org/xsd/LOM domainProfile_1/lomLoose_localised.xsd
                                  http://ltsc.ieee.org/xsd/imscc/LOM domainProfile_2/lomLoose_localised.xsd">
      <xsl:attribute name="identifier">
        <xsl:value-of select="@identifier"/>
      </xsl:attribute>
      <xsl:attribute name="version">
        <xsl:value-of select="@version"/>
      </xsl:attribute>
      <!--rules for manifest metadata are slightly different -->
      <xsl:comment>This has been transfomed by SCORM2004CC</xsl:comment>
      <metadata>
        <schema>IMS Common Cartridge</schema>
        <schemaversion>1.0.0</schemaversion>
        <xsl:apply-templates select="/sco:manifest/sco:metadata/lom:*" />
      </metadata>
      <xsl:apply-templates select="sco:organizations" />
      <xsl:apply-templates select="sco:resources" />
      <!-- Do not match further manifests --> 
    </manifest>
  </xsl:template>
  
  <xsl:template match="lom:*" >
    <xsl:element name="lomcc:{local-name(  )}"><xsl:apply-templates/></xsl:element>
  </xsl:template>
  
  <!-- get rid of all of the lom entries that we don't want -->
  <xsl:template match="lom:lom/lom:metaMetadata" />
  <xsl:template match="lom:lom/lom:annotation" />
  <xsl:template match="lom:lom/lom:general/lom:structure" />
  <xsl:template match="lom:lom/lom:general/lom:aggregationLevel" />
  <xsl:template match="lom:lom/lom:lifeCycle/lom:version" />
  <xsl:template match="lom:lom/lom:lifeCycle/lom:status" />
  <xsl:template match="lom:lom/lom:technical/lom:size" />
  <xsl:template match="lom:lom/lom:technical/lom:location" />
  <xsl:template match="lom:lom/lom:technical/lom:requirement" />
  <xsl:template match="lom:lom/lom:technical/lom:installationRemarks" />
  <xsl:template match="lom:lom/lom:technical/lom:otherPlatformRequirements" />
  <xsl:template match="lom:lom/lom:educational/lom:interactivityType" />
  <xsl:template match="lom:lom/lom:educational/lom:interactivityLevel" />
  <xsl:template match="lom:lom/lom:educational/lom:semanticDensity" />
  <xsl:template match="lom:lom/lom:educational/lom:intendedEndUserRole" />
  <xsl:template match="lom:lom/lom:educational/lom:context" />
  <xsl:template match="lom:lom/lom:educational/lom:typicalAgeRange" />
  <xsl:template match="lom:lom/lom:educational/lom:difficulty" />
  <xsl:template match="lom:lom/lom:educational/lom:typicalLearningTime" />
  <xsl:template match="lom:lom/lom:educational/lom:description" />
  <xsl:template match="lom:lom/lom:educational/lom:language" />

  <xsl:template match="sco:organizations">
      <organizations>
        <!-- Just get the default organization -->
        <xsl:apply-templates select="sco:organization[@identifier=$default-org]"/>
      </organizations>
  </xsl:template>
  
  <xsl:template match="sco:title">
    <title>
      <xsl:value-of select="." />
    </title>
  </xsl:template>
  
  <xsl:template match="sco:organization">
            <organization structure="rooted-hierarchy">
              <xsl:attribute name="identifier">
                <xsl:value-of select="@identifier" />
              </xsl:attribute>
              <xsl:apply-templates select="sco:title" />
                <item identifier="cc01">
                  <xsl:apply-templates select="sco:item" />
                </item>
            </organization>
  </xsl:template>
  
  <xsl:template match="sco:item">
    <item>
      <xsl:attribute name="identifier">
        <xsl:value-of select="@identifier" />
      </xsl:attribute>
      <xsl:if test="@identifierref">
        <xsl:attribute name="identifierref">
          <xsl:value-of select="@identifierref"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="sco:title" />
      <xsl:apply-templates select="sco:item" />
    </item>   
  </xsl:template>

  <xsl:template match="sco:resources">
      <resources>
        <xsl:apply-templates select="sco:resource" />
      </resources>
  </xsl:template>
  
  <xsl:template match="sco:resource">
    <resource>
      <xsl:attribute name="identifier">
        <xsl:value-of select="@identifier" />
      </xsl:attribute>
      <xsl:attribute name="type">
        <xsl:value-of select="@type" />
      </xsl:attribute>
      <xsl:if test="@href">
        <xsl:attribute name="href">
          <xsl:value-of select="@href" />
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="sco:file" />
      <xsl:apply-templates select="sco:dependency" />
    </resource>
  </xsl:template>
  
  <xsl:template match="sco:file">
    <file>
      <xsl:attribute name="href" >
        <xsl:value-of select="@href" />
      </xsl:attribute>
    </file>  
  </xsl:template>
  
  <xsl:template match="sco:dependency">
    <dependency>
      <xsl:attribute name="identifierref">
        <xsl:value-of select="@identifierref"/>
      </xsl:attribute>
    </dependency>
  </xsl:template>

</xsl:stylesheet>
